<template>
  <div class="myCollection">
    <van-nav-bar
        title="我的收藏"
        left-text="返回"
        left-arrow
        :placeholder="true"
        :safe-area-inset-top="true"
        @click-left="$router.back()"
    />
    <div class="blue"></div>
    <div class="container">
      <div
          class="result_item"
          v-for="(item, index) in collectData"
          :key="index">
        <div class="item_img">
          <img class="auto-img" :src="item.smallImg"/>
        </div>
        <div class="item_text">
          <div class="cName">
            <span>{{ item.name }}</span>
          </div>
          <div class="eName">
            <span>{{ item.enname | enName }}</span>
          </div>
          <div class="price">
            <div>￥{{ item.price }}</div>
            <div class="icon" @click="notlike(item.pid)">
              <van-icon name="delete-o"/>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import {findAllLike, notlike} from "@/api/product";

export default {
  name: "myCollection",
  data() {
    return {
      collectData: [],
      tokenString: ''
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      let self = this;
      self.tokenString = this.$cookies.get("tokenString");
      findAllLike({
        tokenString: self.tokenString
      }).then(res => {
        self.collectData = res.data.result
      })
    },
    // 取消收藏
    notlike(pid) {
      let self = this;
      let data = {
        appkey: self.appkey,
        pid: pid,
        tokenString: self.tokenString
      };
      notlike(data).then(res => {
        if (res.data.code === 900) {
          self.$toast.success({
            message: '取消收藏成功！',
            duration: 1500
          });
          self.init();
        } else {
          self.$toast.fail(res.data.msg);
          self.init();
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.myCollection {
  .blue {
    background: #0c34ba;
    width: 100%;
    height: 100px;
  }

  .container {
    background: #ffffff;
    margin: -20px 10px 0 10px;
    border-radius: 3%;
    border: 1px solid #ffffff;
    display: flex;
    flex-wrap: wrap;
    justify-content: left;
    padding: 5px;

    .result_item {
      width: 30%;
      margin: 3px;

      .item_text {
        div {
          margin: 3px 0;
        }

        .cName {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .eName {
          color: #999;
          font-size: 12px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .price {
          font-weight: bold;
          color: #0c34ba;
          font-size: 18px;
          display: flex;
          justify-content: space-between;
          align-items: center;

          .icon {
            color: #999999;
          }
        }
      }
    }

  }
}
</style>
